/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JpnlFinalOverlayed.java
 *
 * Created on 16/06/2010, 22:21:35
 */
package systole.view.tabs.finalsegment;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JCheckBox;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import systole.view.charts.XYChart;

/**
 *
 * @author user
 */
public class JpnlFinalOverlayed extends javax.swing.JPanel {

    private XYChart overlayedChart;
    private ChartPanel chart;


    /** Creates new form JpnlFinalOverlayed */
    public JpnlFinalOverlayed() {
        initComponents();
    }

    public void loadSegments(XYChart overlayedSegments) {
        this.jPnlOverlayedChart.removeAll();
        this.jPnlOverlayedChart.setLayout(new BorderLayout());
        this.overlayedChart = overlayedSegments;
        this.overlayedChart.setShowLegend(true);
        this.chart = this.overlayedChart.plot();
        this.jPnlOverlayedChart.add(this.chart, BorderLayout.CENTER); //Agrego el chart
        this.addCheckboxes();//construyo los checkboxes con funcionalidad
        this.revalidate();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPnlOverlayedChart = new javax.swing.JPanel();
        jPnlOverlayedToggle = new javax.swing.JPanel();
        jPnlTop = new javax.swing.JPanel();
        jPnlRigth = new javax.swing.JPanel();
        jPnlLeft = new javax.swing.JPanel();

        setName("Form"); // NOI18N
        setLayout(new java.awt.BorderLayout());

        jPnlOverlayedChart.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPnlOverlayedChart.setName("jPnlOverlayedChart"); // NOI18N
        jPnlOverlayedChart.setLayout(new java.awt.BorderLayout());
        add(jPnlOverlayedChart, java.awt.BorderLayout.CENTER);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(systole.view.SystoleApp.class).getContext().getResourceMap(JpnlFinalOverlayed.class);
        jPnlOverlayedToggle.setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("jPnlOverlayedToggle.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, resourceMap.getFont("jPnlOverlayedToggle.border.titleFont"))); // NOI18N
        jPnlOverlayedToggle.setName("jPnlOverlayedToggle"); // NOI18N
        jPnlOverlayedToggle.setLayout(new javax.swing.BoxLayout(jPnlOverlayedToggle, javax.swing.BoxLayout.LINE_AXIS));
        add(jPnlOverlayedToggle, java.awt.BorderLayout.SOUTH);

        jPnlTop.setName("jPnlTop"); // NOI18N
        jPnlTop.setPreferredSize(new java.awt.Dimension(404, 5));
        jPnlTop.setLayout(new java.awt.BorderLayout());
        add(jPnlTop, java.awt.BorderLayout.PAGE_START);

        jPnlRigth.setName("jPnlRigth"); // NOI18N
        jPnlRigth.setPreferredSize(new java.awt.Dimension(10, 176));

        javax.swing.GroupLayout jPnlRigthLayout = new javax.swing.GroupLayout(jPnlRigth);
        jPnlRigth.setLayout(jPnlRigthLayout);
        jPnlRigthLayout.setHorizontalGroup(
            jPnlRigthLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 10, Short.MAX_VALUE)
        );
        jPnlRigthLayout.setVerticalGroup(
            jPnlRigthLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 268, Short.MAX_VALUE)
        );

        add(jPnlRigth, java.awt.BorderLayout.LINE_END);

        jPnlLeft.setName("jPnlLeft"); // NOI18N
        jPnlLeft.setPreferredSize(new java.awt.Dimension(10, 176));

        javax.swing.GroupLayout jPnlLeftLayout = new javax.swing.GroupLayout(jPnlLeft);
        jPnlLeft.setLayout(jPnlLeftLayout);
        jPnlLeftLayout.setHorizontalGroup(
            jPnlLeftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 10, Short.MAX_VALUE)
        );
        jPnlLeftLayout.setVerticalGroup(
            jPnlLeftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 268, Short.MAX_VALUE)
        );

        add(jPnlLeft, java.awt.BorderLayout.LINE_START);
    }// </editor-fold>//GEN-END:initComponents

    private void addCheckboxes() {
        ArrayList<String> chartKeys = this.overlayedChart.getKeys();
        JCheckBox segment;
        this.jPnlOverlayedToggle.removeAll();
        for (int i = 0; i < chartKeys.size(); i++) {
            segment = new JCheckBox(chartKeys.get(i), true);
            segment.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent ev) {
                    JCheckBox src = (JCheckBox) ev.getSource();
                    XYItemRenderer rend = chart.getChart().getXYPlot().getRenderer();
                    rend.setSeriesVisible(overlayedChart.getSeriesIndex(src.getText()), src.isSelected(), true);
                }
            });
            this.jPnlOverlayedToggle.add(segment);
        }

    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPnlLeft;
    private javax.swing.JPanel jPnlOverlayedChart;
    private javax.swing.JPanel jPnlOverlayedToggle;
    private javax.swing.JPanel jPnlRigth;
    private javax.swing.JPanel jPnlTop;
    // End of variables declaration//GEN-END:variables
}
